//
//  TriggerManager.m
//  MapEdit
//
//  Created by Steven Canfield on 7/15/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "TriggerManager.h"

@implementation TriggerManager

/* Key Value Coding For Conditions And Actions */
/*static NSString * MEConditionLocationKey		= @"MEConditionLocation";
static NSString * MEConditionGroupKey			= @"MEConditionGroup";
static NSString * MEConditionAmountKey			= @"MEConditionAmount";
static NSString * MEConditionUnitTypeKey		= @"MEConditionUnitType";
static NSString * MEConditionComparisonTypeKey	= @"MEConditionComparisonType";
static NSString * MEConditionTypeKey			= @"MEConditionType";
static NSString * MEConditionModifierKey		= @"MEConditionModifier";
static NSString * MEConditionFlagsKey			= @"MEConditionFlags";

static NSString * MEActionSourceLocationKey		= @"MEActionSourceLocation";
static NSString * MEActionTextKey				= @"MEActionText";
static NSString * MEActionSoundKey				= @"MEActionSound";
static NSString * MEActionTimeKey				= @"MEActionTime";
static NSString * MEActionPrimaryGroupKey		= @"MEActionPrimaryGroup";
static NSString * MEActionSecondaryGroupKey		= @"MEActionSecondaryGroup";
static NSString * MEActionModifierKey			= @"MEActionModififer";
static NSString * MEActionTypeKey				= @"MEActionType";
static NSString * MEActionNumberKey				= @"MEActionNumber";
static NSString * MEActionFlagsKey				= @"MEActionFlags";
*/
/* Interface Descriptor Keys */
/*
static NSString * MEPlayerListKey				= @"MEPlayerList";
static NSString * MENumberKey					= @"MENumber";
static NSString * MEUnitListKey					= @"MEUnitList";
static NSString * MEResourceTypeKey				= @"MEResourceType";
static NSString * MEScoreTypeKey				= @"MEScoreType";
static NSString * MESwitchConditionKey			= @"MESwitchCondition";
static NSString * MEAllianceStatusKey			= @"MEAllianceStatus";
static NSString * MEUnitOrderKey				= @"MEUnitOrder";
static NSString * MESwitchActionKey				= @"MESwitchAction";
static NSString * MENumberComparisonKey			= @"MENumberComparison";
*/
- (id)initWithData:(NSData *)trigData {
	self = [super init];
	if( self ) {
		triggers = malloc( [trigData length] );
		[trigData getBytes:triggers range:NSMakeRange( 0, [trigData length] )];
		trigCount = [trigData length] / sizeof( chk_trig_t );
		
		NSLog(@"%i Triggers", trigCount );
		
		_viewDescriptions = [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewDescriptions" ofType:@"txt"]] componentsSeparatedByString:@"\n"];
		
		//NSLog( @"%@", [_viewDescriptions description] );
		
		[NSBundle loadNibNamed:@"TriggerEditor" owner:self];
		
		//[_window makeKeyAndOrderFront:self];
		
		[_triggerList reloadData];
	}
	return self;
}

- (int)triggerValueForKey:(NSString *)key triggerIndex:(int)trigIndex valueIndex:(int)index {
	return 0;
}

- (void)setValueForKey:(NSString *)key triggerIndex:(int)trigIndex valueIndex:(int)index {
}

- (NSView *)createConditionViewForTriggerAtIndex:(int)trigIndex {
	NSView * view = [[NSView alloc] initWithFrame:NSMakeRect( 0,0,500,100 )];
	
	//NSLog( [view description ]);
	
	int conditionIndex = 0;
	for( conditionIndex = 0; conditionIndex < 16; conditionIndex++ ) {
		if( triggers[ trigIndex ].conditions[ conditionIndex ].condition_type != 0 ) {
			// Create A View For This Condition
			NSView * conditionView = [self createViewForCondition:triggers[ trigIndex ].conditions[ conditionIndex ]];
			NSLog( [conditionView description ]);
		}
	}
	
	return view;
}

- (NSView *)createViewForCondition:(chk_condition_t)condition {
	NSString * descriptor = [_viewDescriptions objectAtIndex: condition.condition_type ];
	NSLog( descriptor );
	
	return NULL;
}

- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
	return trigCount;
}

- (id)tableView:(NSTableView *)tableView
	objectValueForTableColumn:(NSTableColumn *)tableColumn
	row:(int)rowIndex
{
	return [NSNumber numberWithInt:rowIndex];
}

@end
